/*
 * Copyright 2008 Zoundry LLC, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package org.zoundry.gaedt.ui.editors.dialogs;

import java.util.Map;
import org.apache.commons.lang.ArrayUtils;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.zoundry.gaedt.core.models.application.TZGaeHandlerType;
import org.zoundry.gaedt.core.util.ZGaeUtil;

/**
 * The dialog shown to the user when she adds a new handler to
 * an application (by pressing the 'Add' button on the Handlers
 * tab of the Application Editor).  This dialog gathers the
 * information necessary to create a new handler.
 */
public class ZGaeAddHandlerDialog extends Dialog
{
    private static Map<Integer, TZGaeHandlerType> sHandlerMap = ArrayUtils.toMap(
            new Object[][] {
                    {0, TZGaeHandlerType.Script},
                    {1, TZGaeHandlerType.StaticDirectory},
                    {2, TZGaeHandlerType.StaticFilePattern} });


    /** The type of handler to create. */
    private Combo mTypeCombo;
    /** The URL of the new handler. */
    private Text mUrlText;

    /** The result of the dialog. */
    private ZGaeAddHandlerInfo mInfo;

    /**
     * C'tor.
     *
     * @param aShell
     */
    public ZGaeAddHandlerDialog(Shell aShell)
    {
        super(aShell);
    }

    /**
     * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
     */
    @Override
    protected void configureShell(Shell aNewShell)
    {
        aNewShell.setText("Add Handler");
        super.configureShell(aNewShell);
    }

    /**
     * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
     */
    @Override
    protected Control createDialogArea(Composite aParent)
    {
        Composite dialogComposite = (Composite) super.createDialogArea(aParent);
        ((GridLayout) dialogComposite.getLayout()).numColumns = 2;

        Label label = new Label(dialogComposite, SWT.NORMAL);
        label.setText("Handler Type:");
        label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END | GridData.VERTICAL_ALIGN_CENTER));

        mTypeCombo = new Combo(dialogComposite, SWT.DROP_DOWN | SWT.READ_ONLY);
        mTypeCombo.setItems(new String[] { "Script", "Static Directory", "Static Files (pattern)" });
        mTypeCombo.select(0);
        mTypeCombo.setVisibleItemCount(3);
        mTypeCombo.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));

        label = new Label(dialogComposite, SWT.NORMAL);
        label.setText("URL:");
        label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END | GridData.VERTICAL_ALIGN_CENTER));

        mUrlText = new Text(dialogComposite, SWT.NORMAL | SWT.BORDER);
        GridData urlTextLayoutData = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.FILL_HORIZONTAL);
        urlTextLayoutData.widthHint = 200;
        mUrlText.setLayoutData(urlTextLayoutData);

        configureUIListeners();

        return dialogComposite;
    }

    /**
     * Configures listeners on the various UI components.
     */
    private void configureUIListeners()
    {
        mUrlText.addModifyListener(new ModifyListener()
        {
            /**
             * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
             */
            public void modifyText(ModifyEvent aEvent)
            {
                boolean isValid = !ZGaeUtil.isNullOrEmpty(mUrlText.getText());
                getButton(OK).setEnabled(isValid);
            }
        });
    }

    /**
     * @see org.eclipse.jface.dialogs.Dialog#okPressed()
     */
    @Override
    protected void okPressed()
    {
        mInfo = new ZGaeAddHandlerInfo();
        mInfo.setUrl(mUrlText.getText());
        mInfo.setHandlerType(sHandlerMap.get(mTypeCombo.getSelectionIndex()));

        super.okPressed();
    }

    /**
     * @see org.eclipse.jface.dialogs.Dialog#create()
     */
    @Override
    public void create()
    {
        super.create();

        getButton(OK).setEnabled(false);
    }

    /**
     * Gets the add handler info which is the result of this dialog.
     */
    public ZGaeAddHandlerInfo getAddHandlerInfo()
    {
        return mInfo;
    }
}
